<template>
    <div class="student-manage">
        <!-- 面包屑导航 -->
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item>首页</el-breadcrumb-item>
            <el-breadcrumb-item>学生管理</el-breadcrumb-item>
        </el-breadcrumb>

        <!-- 卡片视图 -->
        <el-card shadow="hover">
            <el-table :data="list" border style="width: 100%">
                <el-table-column prop="relname" label="学生姓名" width="180">
                </el-table-column>
                <el-table-column prop="rname" label="学生身份" width="180">
                    <template slot-scope="scope">
                        <el-tag
                            v-if="scope.row.rid === 2"
                            type="warning"
                            size="mini"
                            >学委</el-tag
                        >
                        <el-tag
                            v-else-if="scope.row.rid === 3"
                            type="info"
                            size="mini"
                            >学生</el-tag
                        >
                        <el-tag v-else>未知</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="操作">
                    <template slot-scope="scope">
                        <el-button
                            size="mini"
                            @click="handleEdit(scope.$index, scope.row)"
                            >Edit</el-button
                        >
                        <el-button
                            size="mini"
                            type="danger"
                            @click="handleDelete(scope.row)"
                            >Delete</el-button
                        >
                    </template>
                </el-table-column>
            </el-table>

            <!-- 分页 -->
            <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page.sync="queryInfo.pagenum"
                :page-sizes="[1, 2, 5, 10, 50]"
                :page-size.sync="queryInfo.pagesize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
            >
            </el-pagination>
        </el-card>
    </div>
</template>

<script>
export default {
    mounted() {
        this.getStudentList();
    },
    data() {
        return {
            list: [],
            queryInfo: {
                pagenum: 1,
                pagesize: 10,
            },
            total: 0,
        };
    },
    methods: {
        async getStudentList() {
            let res = await this.$http.get('/users/student', {
                params: this.queryInfo,
            });
            // console.log(res);
            this.list = res.data.list;
            this.total = res.data.total;
        },
        handleEdit(index, row) {
            console.log(index, row);
        },
        handleDelete(row) {
            this.$confirm(
                `此操作将永久删除《${row.relname}》学生, 是否继续?`,
                '提示',
                {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning',
                },
            )
                .then(async () => {
                    let res = await this.$http.delete(
                        `/users/student/${row.uid}`,
                    );
                    if (res.code !== 204) {
                        this.$message.error(res.message);
                        return;
                    }
                    this.$message.success(res.message);
                    this.getStudentList();
                })
                .catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除',
                    });
                });
        },
        handleSizeChange() {
            // console.log('this.queryInfo', this.queryInfo);
            this.getStudentList();
        },
        handleCurrentChange() {
            // console.log('this.queryInfo', this.queryInfo);
            this.getStudentList();
        },
    },
};
</script>
